﻿#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;

#endregion

namespace VisualWebGuiApplication2
{
    public partial class TabkIntegrationBeforFix : UserControl
    {
        int lastid = 0;
      /*  BindingSource _bsTankOpen;
        GazOnDevEntities db2 = new GazOnDevEntities();*/
        Tank_Stocks newrecord;
        Configdb dbconf;
        bool edit = false;


        public List<GazCompany> GetGazCompanies(string Type)
        {
            List<GazCompany> _Accompanying_stocks = new List<GazCompany>();
            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                      var query = from p in db.GazCompanies
                                  where p.type ==Type
                                  select p;
                _Accompanying_stocks = query.ToList<GazCompany>();
            }
            return _Accompanying_stocks;
        }
        public TabkIntegrationBeforFix()
        {
            InitializeComponent();
            this.RightToLeft = Gizmox.WebGUI.Forms.RightToLeft.Yes;
            txtTankNumber.Leave += new EventHandler(txtTankNumber_Leave);
            
            using (GazOnDevEntities db = new GazOnDevEntities())
            {
                dbconf = db.Configdbs.FirstOrDefault();
                txtfixnumber.Text = dbconf.tankfixformnum;
                txtcreatedby.Text = string.Format("{0}", Session["username"]);
              //  txtTankNumber.Text = dbconf.tanknumber;
            }
        }

        public TabkIntegrationBeforFix(string tankid)
        {
            InitializeComponent();
          
            this.RightToLeft = Gizmox.WebGUI.Forms.RightToLeft.Yes;
            txtTankNumber.Leave += new EventHandler(txtTankNumber_Leave);
            txtTankNumber.Text = tankid;
            txtcreatedby.Text = string.Format("{0}", Session["username"]);
            EventArgs ev = new EventArgs();
            txtTankNumber_Leave(txtTankNumber, ev);
        }

        void txtTankNumber_Leave(object sender, EventArgs e)
        {
            using (GazOnDevEntities db = new GazOnDevEntities())
            {


              

                

             //   newrecord = db.Tank_Stocks.FirstOrDefault(x => (x.TankNumber == txtTankNumber.Text));

                newrecord = db.Tank_Stocks.FirstOrDefault(x => (x.TankNumber == txtTankNumber.Text));
                if (null == newrecord) return;
                if (newrecord.Id > 0)
                {
                    if (newrecord.TankSystemStatus == "2" || newrecord.TankSystemStatus == "3" || newrecord.TankSystemStatus == "4" || newrecord.TankSystemStatus == "5" || newrecord.TankSystemStatus == "6")
                    {

                        MessageBox.Show("צובר קיים במערכת אתה עובר למצב עריכה ");
                        edit = true;
                    }
                    if (newrecord.TankSystemStatus == "1")
                    {
                        MessageBox.Show("צובר קיים במערכת בסטאטוס  קיים במלאי  אי אפשר להביא את הנתונים שלו מכאן נא לגשת לטופס פתיחת צובר חדש ");
                        txtTankNumber.Text = "";
                        txtTankNumber.Focus();
                        return;
                    }
                    //if (newrecord.TankSystemStatus == "3")
                    //{
                    //    MessageBox.Show("צובר קיים במערכת אבל הוא בשיפוץ אי אפשר להביא את הנתונים שלו מכאן נא לגשת  לטופס שיפוץ ");
                    //    txtTankNumber.Text = "";
                    //    txtTankNumber.Focus();
                    //    return;
                    //}
                    if (string.IsNullOrEmpty(newrecord.TankNumber))
                    {
                        MessageBox.Show("מספר צובר ריק נא להכניס מספר צובר תקין");
                        txtTankNumber.Text = "";
                        txtTankNumber.Focus();
                        return;
                    }

                }
                else
                {
                    // MessageBox.Show(" אין צובר עם המספר הזה ");
                    // true צובר חדש 
                    edit = false;
                }


                if (null != newrecord)
                {


                   
                       
                        txtTankNumber.Text = newrecord.TankNumber;
                          txtfixnumber.Text=newrecord.TankFixNumber;
                        if (!string.IsNullOrEmpty(newrecord.type))
                        {
                            comboBox1.SelectedIndex = Convert.ToInt32(newrecord.type);
                        }
                 
                         txtdate1.Text = newrecord.Datetime;
                        txtGazCompanyName.Text = newrecord.GazCompanyName;
                        txtGazCompanyAddress.Text = newrecord.GazCompanyAddress;
                        txtGazDistributor.Text = newrecord.GazDistributor;
                        //ddlGazDistributor.SelectedValue = newrecord.GazDistributor;
                        txtnote.Text = newrecord.note;
                        if (null != newrecord.Year_of_manufacture)
                        {
                            txtYear_of_manufacture.Text = newrecord.Year_of_manufacture.ToString();
                        }
                        Date_Of_Fix.Text = newrecord.Date_of_Fix;
                        txtmanufacture_name.Text = newrecord.manufacture_name;
                        if (null != newrecord.Tank_Size)
                        {
                            txt_Tank_Size.Text = newrecord.Tank_Size.ToString();
                        }
                        if (null != newrecord.Label_Number)
                        {
                            txtLabel_Number.Text = newrecord.Label_Number.ToString();
                        }
                        txt_tank_fromAddress.Text = newrecord.Address_Tank_From;
                        if (null != newrecord.NumberOfZoklokPort)
                        {
                            txtNumberOfZoklokPort.Text = newrecord.NumberOfZoklokPort.ToString();
                        }
                        if (null != newrecord.tankStatus)
                        {
                            combotankStatus.SelectedIndex = Convert.ToInt32(newrecord.tankStatus);
                        }
                        checkBox1.Checked = (bool)newrecord.TankWithBase_baton;

                        if (string.IsNullOrEmpty(newrecord.cteatedby))
                        {
                            txtcreatedby.Text = string.Format("{0}", Session["username"]);
                        }
                        else
                        {
                            txtcreatedby.Text = newrecord.cteatedby;
                        }

                        //signed person 
                        if (!string.IsNullOrEmpty(newrecord.PersonRecipetsignName))
                        {
                            txtName_of_recipient.Text = newrecord.PersonRecipetsignName;
                        }
                        if (!string.IsNullOrEmpty(newrecord.PersonRecipetsignSigniture))
                        {
                             txtSign_of_recipient.Text=newrecord.PersonRecipetsignSigniture;
                        }

                        // transport Company

                        if (!string.IsNullOrEmpty( newrecord.TransportCompanyName))
                        {
                            txtTransportCompany.Text= newrecord.TransportCompanyName;
                        }
                        if (!string.IsNullOrEmpty(newrecord.TransportCompanyTransportername))
                        {
                            txtTransporterName.Text = newrecord.TransportCompanyTransportername;
                        }
                        if (!string.IsNullOrEmpty(newrecord.CarNumber))
                        {
                            txtCarNumber.Text = newrecord.CarNumber;
                        }


                      
                  
                }

            }

        }

        private void btnOpenTank_Click(object sender, EventArgs e)
        {
          
           

            using (GazOnDevEntities db = new GazOnDevEntities())
            {


                if (string.IsNullOrEmpty(txtTankNumber.Text))
                {
                    MessageBox.Show("מספר צובר ריק נא להכניס מספר צובר תקין");
                    txtTankNumber.Text = "";
                    txtTankNumber.Focus();
                    return;
                }


                 newrecord = db.Tank_Stocks.FirstOrDefault(x => (x.TankNumber == txtTankNumber.Text));
                 if (null != newrecord)
                {
                    if (!edit)
                    {
                        MessageBox.Show("צובר קיים אי אפשר להוסיף אותו פעמיים");
                        return;
                    }
                } 

                if(null !=newrecord)
                {
                    
              
                if (string.IsNullOrEmpty(newrecord.TankNumber))
                {
                    MessageBox.Show("מספר צובר ריק נא להכניס מספר צובר תקין");
                    txtTankNumber.Text = "";
                    txtTankNumber.Focus();
                    return;
                }
                }
                if (null == newrecord)
                {
                    edit = false;
                }
                if (edit)
                {

                   // dbconf.tanknumber = (Convert.ToInt32(dbconf.tanknumber) + 1).ToString();
                    if (null != dbconf)
                    {
                        dbconf.tankfixformnum = (Convert.ToInt32(dbconf.tankfixformnum) + 1).ToString();
                       
                        try
                        {
                            db.Attach(dbconf);
                            db.ObjectStateManager.ChangeObjectState(dbconf, EntityState.Modified);
                        }
                        catch (Exception ddd) { }
                        
                    }
                   
                   
                       
                    

                }
                else
                {

                    newrecord = new Tank_Stocks();
                }


                    newrecord.TankNumber = txtTankNumber.Text;
                    newrecord.TankFixNumber = txtfixnumber.Text;
                    newrecord.type = comboBox1.SelectedIndex.ToString();
                    newrecord.Datetime = txtdate1.Text;
                    newrecord.GazCompanyName = txtGazCompanyName.Text;
                    newrecord.GazCompanyAddress = txtGazCompanyAddress.Text;
                    newrecord.GazDistributor = txtGazDistributor.Text;
                  
                  
                    newrecord.note = txtnote.Text;
                    newrecord.cteatedby=txtcreatedby.Text;
                    if (!string.IsNullOrEmpty(txtYear_of_manufacture.Text))
                    {
                        newrecord.Year_of_manufacture = Convert.ToInt32(txtYear_of_manufacture.Text);
                    }
                    newrecord.Date_of_Fix = Date_Of_Fix.Text;
                    newrecord.manufacture_name = txtmanufacture_name.Text;
                    if (!string.IsNullOrEmpty(txt_Tank_Size.Text))
                    {
                        newrecord.Tank_Size =txt_Tank_Size.Text;
                    }
                    if (!string.IsNullOrEmpty(txtLabel_Number.Text))
                    {
                        newrecord.Label_Number = Convert.ToInt32(txtLabel_Number.Text);
                    }
                    newrecord.Address_Tank_From = txt_tank_fromAddress.Text;
                    if (!string.IsNullOrEmpty(txtNumberOfZoklokPort.Text))
                    {
                        newrecord.NumberOfZoklokPort = Convert.ToInt32(txtNumberOfZoklokPort.Text);
                    }
                    newrecord.tankStatus = combotankStatus.SelectedIndex.ToString();
                    newrecord.TankWithBase_baton = checkBox1.Checked ? true : false;
                    // means that enabled at the stock  
                    //status 2 means that the tank is ready for fix and not in the stock
                    if (newrecord.TankSystemStatus != "3" && newrecord.TankSystemStatus != "4" && newrecord.TankSystemStatus != "5" && newrecord.TankSystemStatus != "6")
                    {
                        newrecord.TankSystemStatus = "2";
                    }

                    

                    //signed person 
                    if (!string.IsNullOrEmpty(txtName_of_recipient.Text))
                    {
                        newrecord.PersonRecipetsignName = txtName_of_recipient.Text;
                    }
                    if (!string.IsNullOrEmpty(txtSign_of_recipient.Text))
                    {
                        newrecord.PersonRecipetsignSigniture = txtSign_of_recipient.Text;
                    }

                    // transport Company

                    if (!string.IsNullOrEmpty(txtTransportCompany.Text))
                    {
                        newrecord.TransportCompanyName = txtTransportCompany.Text;
                    }
                    if (!string.IsNullOrEmpty(txtTransporterName.Text))
                    {
                        newrecord.TransportCompanyTransportername = txtTransporterName.Text;
                    }
                    if (!string.IsNullOrEmpty(txtCarNumber.Text))
                    {
                        newrecord.CarNumber = txtCarNumber.Text;
                    }
                    if (!edit)
                    {
                        dbconf.tanknumber = (Convert.ToInt32(dbconf.tanknumber) + 1).ToString();
                        dbconf.tankfixformnum = (Convert.ToInt32(dbconf.tankfixformnum) + 1).ToString();
                        db.Attach(dbconf);
                        db.ObjectStateManager.ChangeObjectState(dbconf, EntityState.Modified);


                        db.AddToTank_Stocks(newrecord);
                    }
                    else {

                        try
                        {
                            db.Attach(newrecord);
                            db.ObjectStateManager.ChangeObjectState(newrecord, EntityState.Modified);
                        }
                        catch (Exception ddd)
                        {

                            db.ApplyOriginalValues("Tank_Stocks", newrecord);
                        }
                    }
                   
                
                if (db.SaveChanges() > 0)
                {
                      MessageBox.Show("פעול  הצליחה");
                    //foreach (Control item in this.Controls)
                    //{
                    //    item.Enabled = false;
                    //    //if(item.)

                    //}
                    //btnOpenTank.Enabled = false;
                  
                }

            }
            
        }

        void ShowReport(int reportid)
        {
            frmReportTankRecipet tanko = new frmReportTankRecipet(reportid, "report1");
            tanko.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(null!=newrecord)
            {
                ShowReport(newrecord.Id);

            }else

            {
            MessageBox.Show("נא לבחור צובר ");
            }
            
        }
    }
}